//
//  SISSReceivedRequestViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/8.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSReceivedRequestViewController.h"
#import "SISSReceivedRequestCell.h"
#import "SISSNetworkingManager+MineCooperation.h"
#import "SISSReleaseDetailViewController.h"
#import "SISSReleasePassengerDeatailViewController.h"
#import "SISSChatViewController.h"
#import "SISSHouseCooperationProgressViewController.h"
#import "SISSPassengerCooperationProgressViewController.h"

static NSString *receivedRequestCellReuseID = @"receivedRequestCellReuseID";
@interface SISSReceivedRequestViewController ()<SISSReceivedRequestCellDelegate>

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/* 中介信息模型数组 */
@property (strong, nonatomic) NSArray<SISSAgentInfoModel *> *agentInfoArr;

/* 标记是否是房源合作 */
@property (assign, nonatomic) BOOL isHouseCooperation;

@end

@implementation SISSReceivedRequestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"收到的请求";
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}



#pragma mark - 下拉刷新加载方法
- (void)upDownLoadData {
    
    
    [self requestReceivedHouseCooperationRequestData];
}


#pragma mark - 加载数据
/**
 房源合作
 */
- (void)requestReceivedHouseCooperationRequestData {
    
    [kNetworkManager requestHouseCooperationCooperationRequestWithCooperationId:self.ahcrId success:^(NSArray<SISSAgentInfoModel *> *agentInfos) {
        
        self.agentInfoArr = agentInfos;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}

/**
 客源合作
 */
- (void)requestReceivedPassengerCooperationRequestData {

    [kNetworkManager requestPassengerCooperatioonCooperationRequestWithCooperationId:self.adcrId success:^(NSArray<SISSAgentInfoModel *> *agentInfos) {
        
        self.agentInfoArr = agentInfos;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.agentInfoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSReceivedRequestCell *cell = [tableView dequeueReusableCellWithIdentifier:receivedRequestCellReuseID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.agentInfoModel = self.agentInfoArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - SISSReceivedRequestCellDelegate
- (void)receivedRequestCell:(SISSReceivedRequestCell *)cell didClickButtonAtIndex:(NSInteger)index {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    if (index == 0) { // 咨询
        
        SISSChatViewController *chatVC = [[SISSChatViewController alloc] initWithConversationChatter:self.agentInfoArr[indexPath.row].imAccount conversationType:EMConversationTypeChat];
        SISSContactModel *model = [[SISSContactModel alloc] init];
        model.name = self.agentInfoArr[indexPath.row].name;
        model.portrait = self.agentInfoArr[indexPath.row].portrait;
        model.spId = self.agentInfoArr[indexPath.row].spId;
        model.accountType = 1;
        chatVC.contactModel = model;
        chatVC.title = self.agentInfoArr[indexPath.row].name;
        [self.navigationController pushViewController:chatVC animated:YES];
        
    } else { // 同意、不同意 index 0咨询 1不同意 2 同意
        
        // status 1同意 2不同意
        NSInteger status = index == 1 ? 2 : 1;
        [SISSPublicSingleton shared].isHud = NO;//
        if (self.isHouseCooperation) {
            
            [kNetworkManager updateHouseCooperationCooperationRequestWithCooperationId:self.ahcrId agentModel:self.agentInfoArr[indexPath.row] status:status success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    NSLog(@"%@",responseObject[@"message"]);
                    
                    [SISSTools showSVTitle:@"操作成功！" andTime:1 andColor:YES];
                    if (status == 1) {
                        [self requestReceivedHouseCooperationRequestData];

                        // 跳转 合作进度详情
//                        SISSHouseCooperationProgressViewController *vc = [[SISSHouseCooperationProgressViewController alloc] init];
////                        vc.
//                        [self.navigationController pushViewController:vc animated:YES];
                    } else {
                        [self requestReceivedHouseCooperationRequestData];
                    }
                }
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
            
        } else {
            
            [kNetworkManager updatePassengerCooperatioonCooperationRequestWithCooperationId:self.adcrId agentModel:self.agentInfoArr[indexPath.row] status:status success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    NSLog(@"%@",responseObject[@"message"]);
                    
                    [SISSTools showSVTitle:@"操作成功！" andTime:1 andColor:YES];
                    if (status == 1) {
                        [self requestReceivedPassengerCooperationRequestData];
                        // 跳转 合作进度详情
//                        SISSPassengerCooperationProgressViewController *vc = [[SISSPassengerCooperationProgressViewController alloc] init];
////                        vc.
//                        [self.navigationController pushViewController:vc animated:YES];
                    } else {
                        [self requestReceivedPassengerCooperationRequestData];
                    }
                }
            } failed:^(NSError *error) {
                
                NSLog(@"%@",error);
            }];
        }
    }
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 125;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSReceivedRequestCell" bundle:nil] forCellReuseIdentifier:receivedRequestCellReuseID];
}

// 客源合作id
- (void)setAdcrId:(NSString *)adcrId {
    _adcrId = adcrId;
    
    self.isHouseCooperation = NO;
    [self requestReceivedPassengerCooperationRequestData];
}

// 房源合作id
- (void)setAhcrId:(NSString *)ahcrId {
    _ahcrId = ahcrId;
    
    self.isHouseCooperation = YES;
    [self requestReceivedHouseCooperationRequestData];
}

/// 点击跳转发布详情
- (IBAction)releaseDeatilTapGestureActionClick:(id)sender {
    if (self.isHouseCooperation) {
        
        SISSReleaseDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"SISSReleaseDetailViewController"];
        vc.ahcrId = self.ahcrId;
        WeakSelf
        vc.deleteComplete = ^{ // 删除回调
            
            if (weakSelf.deleteComplete) {
                weakSelf.deleteComplete();
            }
        };
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        
        SISSReleasePassengerDeatailViewController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"SISSReleasePassengerDeatailViewController"];
        vc.adcrId = self.adcrId;
        WeakSelf
        vc.deleteComplete = ^{ // 删除回调
            
            if (weakSelf.deleteComplete) {
                weakSelf.deleteComplete();
            }
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}


@end
